%-------------------------------------------------------------------------------
% $Id: wav1d_dalembert.m,v 1.2 2011/06/20 21:53:38 paul Exp $
% $Date: 2011/06/20 21:53:38 $
% $Author: paul $
%
%% d'Alembert's Solution to 1D wave equation of a vibrating string
%-------------------------------------------------------------------------------
syms t x real                  % t=time , x=distance

total_length = 1.0             % length of string on x-axis

f = exp(-1.0/(1-(10*x-1)^2));  % initial disturbance function
disturbance_length = 0.2;      % interval of disturbance funtion is (0,0.2)

window = heaviside(x)-heaviside(x-disturbance_length);
fwindow = f * window;
fodd = fwindow - subs(fwindow,x,-x);

period = 2*total_length;
fperiodic = fodd + subs(fodd,x,x-period) + subs(fodd,x,x+period);

c = 1.0;

% d'Alembert's solution
y = 0.5 * (subs(fperiodic,x,x+c*t) + subs(fperiodic,x,x-c*t)) 

%-------------------------------------------------------------------------------
% Setup AVI file and parameters
aviobj = avifile('wav1d_dalembert.avi');
aviobj.compression = 'None';             % no other option for Unix
aviobj.fps = 5;                          % frames per second
aviobj.quality = 10;                     % low quality is OK

%-------------------------------------------------------------------------------
% Animation loop
fig = figure;

M = 500;                       % number of samples in time interval
T = 2;                         % time interval (0,T)
dt = T / M;

for time=0:dt:T
  ezplot(subs(y,t,time),[0 1]);
  axis([0 1 -.4 .4]);
  title('1D Wave Equation d_Alembert Solution') 
  frame = getframe(fig); 
  aviobj = addframe(aviobj,frame);
end 

%-------------------------------------------------------------------------------
% Clean up rendering frame and AVI file

close(fig);
aviobj = close(aviobj);